from scrapy import Spider, Request

from urllib.parse import urlparse
from ..items import ImgItem

class DeskimgSpider(Spider):
    name = 'deskimg'
    allowed_domains = ['desk.zol.com.cn']
    start_urls = ['http://desk.zol.com.cn/']
    def imgItem(self,response):
        item = ImgItem()
        item["name"] = response.xpath('//*[@id="titleName"]')[0].root.text
        item["src"] = response.url
        return  item
        pass

    def parse_item(self, response):
        alist = response.css("html body div.wrapper.top-main.clearfix div.main ul.pic-list2.clearfix")[0].css("a")
        for a in alist:
            href = a.attrib['href']
            yield  Request(url=self.urlJoin(response.url,href),callback=self.imgItem)
            pass
        pass

    def parse(self, response):
        alist = response.css(
            "html body div.wrapper.clearfix div#main.main.choosebox.mt15 dl.filter-item.first.clearfix dd.brand-sel-box.clearfix")[
            0].css('a')
        for a in alist:
            href = a.attrib["href"]
            # 递归提取
            yield Request(self.urlJoin(response.url, href), callback=self.parse_item)

        pass

    def urlJoin(self, url, href):
        urlbase = urlparse(url)
        return "{}://{}{}".format(urlbase.scheme, urlbase.hostname, href)